// 封装请求：get post

// 引入错诶提示
import { showToast } from './showToast'


// get
const getRequest = function (url) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: url,
      method: 'GET',
      header: {
        'Authorization': uni.getStorageSync('token'),
      }
    }).then((res) => {
      resolve(res)
    }).catch((err) => {
      showToast.message('服务器连接失败 请稍后再试')
      reject(err)
    })
  })
}
// post
const postRequest = function (url, data) {
  return new Promise((resolve, reject) => {
    uni.request({
      method: 'POST',
      url: url,
      data: data,
      header: {
        'Authorization': uni.getStorageSync('token'),
        'content-type': 'application/x-www-form-urlencoded'
      }
    }).then((res) => {
      resolve(res)
      // showToast.message('登录成功')
    }).catch((err) => {
      showToast.message('服务器连接失败 请稍后再试')
      reject(err)
    })
  })
}

const http = (url, data, method) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: url,
      data: data || {},
      method: method || 'GET',
      header: {
        'Authorization': uni.getStorageSync('token'),

      },
    }).then((res) => {
      console.log('======here=======')
      resolve(res)
    }).catch((err) => {
      showToast.message('服务器连接失败 请稍后再试')
      reject(err)
    })
  })
}

/* 
const http = (options) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: {
        Authorization: uni.getStorageSync('token'),
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: (res) => {
        if (res == '') {
          return uni.showToast({
            icon: 'loading',
            title: '获取数据失败'
          })
        }
        resolve(res)
      },
      fail: (err) => {
        return uni.showToast({
          icon: 'loading',
          title: '请求失败'
        })
        reject(err)
      }
    })
  })
}
*/

// getAlarmInfo
const getAlarmInfo = function (url) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: url, //设备API地址
      data: {
      },
      header: {
        "content-type": "application/x-www-form-urlencoded", // http头部
        "api-key": "OYkrDZ2fXos5LNzh=axsWrMwIPE=",
      },
    }).then((res) => {
      resolve(res)
    }).catch((err) => {
      showToast.message('服务器连接失败 请稍后再试')
      reject(err)
    })
  })
}

// postAlarmStatus
const postAlarmStatus = function (url, data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: url,
      method: "POST",
      header: {
        "content-type": "application/json", // http头部
        "api-key": "OYkrDZ2fXos5LNzh=axsWrMwIPE=",
      },
      data: data,
    }).then((res) => {
      resolve(res)
      // showToast.message('登录成功')
    }).catch((err) => {
      showToast.message('服务器连接失败 请稍后再试')
      reject(err)
    })
  })
}

export { getRequest, postRequest, getAlarmInfo, postAlarmStatus }